﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management;
using System.Security.Cryptography;
using System.Diagnostics;

namespace RI
{
    public partial class frm_Login : Form
    {
        public frm_Login()
        {
            InitializeComponent();
            txtKey.Text = GetMD5(GetNetworkMAC());   //获取本机MD5

            //using (SystemConfig sc = new SystemConfig())
            //{
            //    this.Cursor = sc.SetCurrentCursor("Skins/Arrow.ani", this.Cursor);
            //    foreach(Control co in groupBox1.Controls)
            //    {
            //        if (co is TextBox)
            //        {
            //            co.Cursor = sc.SetCurrentCursor("Skins/IBeam.ani", co.Cursor);
            //        }
            //    }
            //}
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
            Application.Exit();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            string md5Me = "711A2024254CBF8116479032A5D1A66D"; //作者

            if (txtUser.Text.Trim() == "admin" &&
                txtPassword.Text.Trim() == "admin" &&
                txtKey.Text.Trim() == md5Me)
            {
                this.Hide();
                this.ShowInTaskbar = false;
                fromMain main = new fromMain();
                main.Show();
            }
            else
            {
                MessageBox.Show("激活失败!", "提示",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }
        }

        private string GetNetworkMAC()
        {
            string strMac = string.Empty;
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if ((bool)mo["IPEnabled"] == true)
                    strMac += mo["MacAddress"].ToString();

            }
            return strMac.ToUpper();
        }

        private string GetMD5(string source)
        {
            string pwd = "";
            MD5 md5 = MD5.Create();
            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(source));
            for (int i = 0; i < s.Length; i++)
            {
                pwd = pwd + s[i].ToString("X");

            }
            return pwd;
        }

        private void frm_Login_Load(object sender, EventArgs e)
        {
            Process[] myProcess = Process.GetProcesses();
            int pNum = 0;
            foreach (Process myProc in myProcess)
            {
                if (myProc.ProcessName == "亲亲宝贝")
                {
                    pNum++;
                }
            }
            if (pNum > 1)
            {
                MessageBox.Show("请先关闭现有的程序!", "提示",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Information);
                Application.Exit();
            }
        }
    }
}
